package com.cph.contentcenter.service.content;

import com.cph.contentcenter.dao.content.ShareMapper;
import com.cph.contentcenter.domain.dto.content.ShareDto;
import com.cph.contentcenter.domain.dto.user.UserDto;
import com.cph.contentcenter.domain.entity.content.Share;
import com.cph.contentcenter.feignClinet.UserCenterFeignClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;

/**
 * @ClassName ShareService
 * @Description TODO
 * @Author cph
 * @Date 2020/9/25 17:43
 * @Version 1.0
 */
@Service
public class ShareService {

    @Autowired
    private ShareMapper shareMapper;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private UserCenterFeignClient userCenterFeignClient;

//    @Autowired
//    private DiscoveryClient discoveryClient;

    public ShareDto findById(Integer id) {
        Share share = shareMapper.selectByPrimaryKey(id);
        Integer userId = share.getUserId();

//        List<ServiceInstance> instances = discoveryClient.getInstances("user-center");
//       //所有用户中心的地址
//        List<String> targetUrl = instances.stream().map(instance->instance.getUri().toString()+"/users/{id}").collect(Collectors.toList());
//        int i = ThreadLocalRandom.current().nextInt(targetUrl.size());
//        //用HTTP GET请求
//        UserDto userDto =  restTemplate.getForObject("http://user-center/users/{userId}",UserDto.class,userId);

        UserDto userDto = userCenterFeignClient.findById(id);

        //消息的装配
        ShareDto shareDto = new ShareDto();
        BeanUtils.copyProperties(share, shareDto);
        shareDto.setWxNickname(userDto.getWxNickname());
        return shareDto;
    }

}
